using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ModelBinder.Host.Infrastructures.Middlewares;
using ModelBinder.Host.Infrastructures.ModelBinders;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers(opt =>
{
    opt.ModelBinderProviders.Insert(0, new CustomBodyWrapModelBinderProvider(opt));
    //opt.ModelBinderProviders.Insert(1, new CustomMultiModelBinderProvider(opt));
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var serviceDescriptor = builder.Services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IModelBinderFactory));
builder.Services.Remove(serviceDescriptor);
builder.Services.TryAddSingleton<IModelBinderFactory, CustomModelBinderFactory>();
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseMiddleware<HttpBodyMiddleware>();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

public partial class Program { }